from flask import Flask, request, render_template, redirect
from utils import db


app = Flask(__name__)

@app.route("/index")
def index():
    total_list = db.fetch_all()
    print(total_list)
    all_num, done, achieve = db.get_info()

    return render_template(
        "index.html",
        total_list=total_list,
        all_num=all_num,
        done=done,
        achieve=achieve
    )

@app.route("/add", methods=['GET', 'POST'])
def add_content():
    content = request.form.get('content')
    db.insert(content)
    return redirect('index')


@app.route("/delete")
def list_delete():
    # http://127.0.0.1:5000/delete?order=1
    # 接收nid
    order = request.args.get('order')
    # 执行sql
    db.delete(order)
    # 跳转回列表
    return redirect('index')

@app.route('/changeCondition')
def change_condition():
    condition = request.args.get('condition')
    order = request.args.get('order')
    if condition == 'Done':
        db.change_condition(order, 'Undo')
    else:
        db.change_condition(order, 'Done')
    return redirect('index')


if __name__ == '__main__':
    app.run()


